package edu.csl.study.shardingsphere.health.service.impl;

import edu.csl.study.shardingsphere.health.entity.HealthRecord;
import edu.csl.study.shardingsphere.health.entity.HealthTask;
import edu.csl.study.shardingsphere.health.mapper.HealthRecordMapper;
import edu.csl.study.shardingsphere.health.mapper.HealthTaskMapper;
import edu.csl.study.shardingsphere.health.service.IHealthRecordService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import edu.csl.study.shardingsphere.health.vo.HealthRecordTaskVo;
import edu.csl.study.shardingsphere.transaction.TransactionService;
import org.apache.shardingsphere.transaction.annotation.ShardingTransactionType;
import org.apache.shardingsphere.transaction.core.TransactionType;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 自动生成代码
 * @since 2022-05-19
 */
@Service
public class HealthRecordServiceImpl extends ServiceImpl<HealthRecordMapper, HealthRecord> implements IHealthRecordService {

    @Autowired
    private HealthTaskMapper healthTaskMapper;
    @Autowired
    JdbcTemplate jdbcTemplate;

    @Override
    @Transactional(rollbackFor =Exception.class ) //必须这样，因为默认只回滚RuntimeException
    @ShardingTransactionType(TransactionType.BASE) //指定为XA模式
    public void processHealthRecords() throws SQLException{
        insertHealthRecords();
    }

    @Override
    public void deleteAll() {
        baseMapper.delete(null);
        healthTaskMapper.delete(null);
    }



    private List<Long> insertHealthRecords() throws SQLException {
        List<Long> result = new ArrayList<>(10);
        boolean ex = false;
        for (Long i = 1L; i <= 10; i++) {
            HealthRecord healthRecord = insertHealthRecord(i);
            insertHealthTask(i, healthRecord);
            result.add(healthRecord.getRecordId());
            ex = true;
        }
//        if(ex){
//            throw  new SQLException(" 模拟错误！！！！");
//        }
        return result;
    }

    private HealthRecord insertHealthRecord(final Long i) throws SQLException {
        HealthRecord healthRecord = new HealthRecord();
        healthRecord.setUserId(i);
        healthRecord.setLevelId(i % 5);
        healthRecord.setRemark("Remark" + i);
        baseMapper.insert(healthRecord);
        return healthRecord;
    }

    private void insertHealthTask(final Long i, final HealthRecord healthRecord) throws SQLException {
        HealthTask healthTask = new HealthTask();
        healthTask.setRecordId(healthRecord.getRecordId());
        healthTask.setUserId(i);
        healthTask.setTaskName("TaskName" + i);
        healthTaskMapper.insert(healthTask);
    }
    @Override
    public List<HealthRecordTaskVo> getHealthRecordTaskVo(int type,Long id) {

        String sql = " ";

        if(type == 1){//普通写法
            sql = "select r.record_Id,r.user_id,r.remark,r.level_Id, l.level_name" +
                    " from health_record  r, health_level l where " +
                    " r.level_Id = l.level_id " +
                    " and  r.user_id > ? ";
        }else if(type == 2){ // Join
            sql = "select  r.record_Id,r.user_id,r.remark,r.level_Id, l.level_name " +
                    " from health_record  r  join health_level l  on " +
                    " r.level_Id = l.level_id " +
                    " where  r.user_id > ? ";
        }else if(type == 3){// left Join
            sql = "select r.record_Id,r.user_id,r.remark,r.level_Id, l.level_name " +
                    " from health_record  r left join health_level l  on " +
                    " r.level_Id = l.level_id " +
                    " where  r.user_id > ? ";
        }else if(type == 4){ // left join + 普通写法
            sql = "select r.record_Id,r.user_id,r.remark,r.level_Id,u.user_name ,l.level_name,task.task_name " +
                    " from health_record  r left join health_level l  on " +
                    " r.level_Id = l.level_id , health_task task , user u " +
                    " where  task.record_id = task.record_id " +
                    " and r.user_id =  u.user_id " +
                    " and r.user_id > ? ";
        }else if(type == 5){ // 子查询 - 报错：Table 'ds00.user' doesn't exist，因为子查询支持有问题：没有替换表名称。
            sql = "select r.record_Id,r.user_id,r.remark,r.level_Id  " +
                    " from health_record  r    " +
                    " where r.user_id in ( select user_id from user u where u.user_id > ?) ";
        }else if(type == 6){ // 子查询 - 正常， 因为health_level在数据库中真实存在
            sql = "select r.record_Id,r.user_id,r.remark,r.level_Id  " +
                    " from health_record  r    " +
                    " where r.level_Id in ( select level_Id from health_level u where u.level_Id > ?) ";
        }else if(type == 7){ // 分页
            sql = "select r.record_Id,r.user_id,r.remark,r.level_Id  " +
                    " from health_record  r    " +
                    " where r.user_id > ? limit 1,3 ";
        }else if(type == 8){ // 排序 + 分页
            sql = "select r.record_Id,r.user_id,r.remark,r.level_Id  " +
                    " from health_record  r    " +
                    " where r.user_id > ?  order by r.user_id desc limit 1,3 ";
        }

        List<HealthRecordTaskVo> list = jdbcTemplate.query(sql, new BeanPropertyRowMapper<>(HealthRecordTaskVo.class), id);
        for (HealthRecordTaskVo vo : list) {
            System.out.println(vo);
        }
        return list;
    }

}
